// pages/recommend/index.js
var userInfo = wx.getStorageSync('userInfo');
var url = getApp().globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: {},
    nickName: userInfo.nickName,
    showModalStatus: true,
    cancelText: '使用优惠券',
    confirmText: '分享领红包',
    list: [],
    key: wx.getStorageSync("key"),
    fen_id: 0,
    person: '',
    secretkey: '',
    showModalStatus: false,
    showModalStatu: false,
    share_price: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      fen_id: options.rpacket_t_id,
      person: options.redpacket_share_person,
      secretkey: options.redpacket_share_secretkey
    })
    wx.showLoading({
      title: '加载中，请稍后...',
      mask: true,
    })
    wx.request({
      url: url + '/mobile/index.php?act=index&op=get_free_redpacket_info',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      dataType: "json",
      data: {
        rpacket_t_id: options.rpacket_t_id,
        key: wx.getStorageSync("key"),
      },
      success: (res)=> {
        if (res.data.code == 200) {
          
        }
      }
    })
    // 获取分享红包数据
    wx.request({
      url: url + '/mobile/index.php?act=index&op=get_share_redpacket_info',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      dataType: "json",
      data: {
        key: wx.getStorageSync("key"),
        rpacket_t_id: options.rpacket_t_id,
        redpacket_share_person: options.redpacket_share_person,
        redpacket_share_secretkey: options.redpacket_share_secretkey
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading();
          var img = res.data.share_person_info
          this.setData({
            list: res.data.datas,
            avatar: img,
          })
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none'
        })
      }
    });

    // 获取分享红包的地址、id
    wx.request({
      url: url + '/mobile/index.php?act=api&op=get_banner',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        banner_id: 40
      },
      success: (res) => {
        if (res.data.code == 200) {
          var arr = [];
          for (var i in res.data.datas) {
            arr.push(res.data.datas[i]);
            if (arr[i].adv_id == 19) {
              var adv_id = arr[i].adv_id;
              var adv_url = arr[i].adv_url;
              var fen_id = arr[i].adv_url.slice(-1)
            }
          }
          this.setData({
            fen_id: fen_id,
            fen_url: adv_url,
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          mask: true
        })
      }
    })
    this.getList();
  },
  // 分享红包时携带的参数
  shareAgain: function (e) {
    var key = wx.getStorageSync("key");
    getApp().saveFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中,请稍后...',
      mask: true,
    });
    wx.request({
      url: url + '/mobile/index.php?act=member_redpacket&op=get_share_parameter',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      dataType: "json",
      data: {
        key: key,
        rpacket_t_id: this.data.fen_id,
      },
      success: (res) => {
        if (!key) {
          wx.navigateTo({
            url: '/pages/authorize/index',
          })
        };
        if (res.data.code == 200) {
          wx.hideLoading();
          this.setData({
            person1: res.data.datas.redpacket_share_person,   // 分享红包人id
            secretkey1: res.data.datas.redpacket_share_secretkey,  // 红包分享比对密钥
            showModalStatu: true,
            share_price: res.data.datas.rpacket_t_price
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }
    })
  },
  // 获取分享优惠券
  getList: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var key = wx.getStorageSync("key");
    wx.request({
      url: url + '/mobile/index.php?act=member_redpacket&op=redpacket_list',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      data: {
        key: key,
        rp_state: ''
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading();
          var arr = [];
          var redpacket_list = res.data.datas.redpacket_list;
          for (var count in redpacket_list) {
            arr.push(redpacket_list[count].rpacket_t_id);
          };
          if (arr.length == 2) {
            this.setData({
              showModalStatus: false
            })
          } else {
            this.setData({
              showModalStatus: true
            })
          }
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
        })
      }
    })

  },
  onMyModal: function (e) {
    var currentStatu = e.detail.status;
    switch (currentStatu) {
      case 'open':
        this.setData({
          showModalStatus: true
        })
        break;
      case 'close':
        this.setData({
          showModalStatus: false
        })
        break;
    }
  },
  powerDrawer: function (e) {
    
  },
  cancel: function (e){
    wx.switchTab({
      url: '/pages/index/index',
    });
    this.setData({
      showModalStatus: false
    });
  },
  confirm: function (e) {
    this.setData({
      showModalStatus: false
    });
  },
  // 关闭弹窗
  close: function () {
    this.setData({
      showModalStatu: false
    })
  }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.share){
      this.setData({
        showModalStatus: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showLoading({
      title: '加载中...',
    })
    var red_url = this.data.fen_url + '&redpacket_share_person=' + this.data.person1 + '&redpacket_share_secretkey=' + this.data.secretkey1;
    this.share = true;
    return {
      title: this.data.nickName + '@你,送你一' + this.data.share_price + '张优惠券',
      path: red_url,
      imageUrl: 'https://www.zhuisushangcheng.com/data/upload/applet/redImg.png',
      success: (res) => {
        wx.request({
          url: url + '/mobile/index.php?act=member_redpacket&op=share_redpacket_callback_share',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          dataType: "json",
          data: {
            key: wx.getStorageSync("key"),
            rpacket_t_id: this.data.fen_id,
            redpacket_share_person: this.data.person,
            redpacket_share_secretkey: this.data.secretkey
          },
          success: (res) => {
            if (res.data.code == 200) {
              wx.hideLoading();
              wx.showToast({
                title: res.data.datas,
                duration: 2000,
              })
              wx.navigateTo({
                url: red_url,
              })
            };
            if (res.data.code == 400) {
              wx.showToast({
                title: res.data.datas.error,
                duration: 2000,
                icon: "none"
              });
            }
          },
          fail: () => {
            wx.showToast({
              title: '人太多啦',
              icon: 'none',
              mask: true
            })
          }
        });
        wx.navigateTo({
          url: red_url,
        })
      },
      fail: () => {
        wx.showToast({
          icon: 'none',
          title: '取消分享',
          duration: 2000,
        });
        this.setData({
          showModalStatu: false
        })
      }
    }
  },
  // 去使用优惠券跳转
  toUse: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})